<?php

namespace app\sales\controller;

use app\admin\common\Base;
use app\scm\model\PurchaseSalesList;
use think\Db;
//销售利润表
class Saleprofitdetail extends Base
{
   public function index()
   {
      $data = $this->request->param();

      if (isset($data['ajax']) && $data['ajax'] == 100) {
         $map = '';
         $page = isset($data['page']) ? $data['page'] : 1;
         $page = intval($page);
         $limit = isset($data['rows']) ? $data['rows'] : 10;
         $limit = intval($limit);
         $start = $limit * ($page - 1);


         if ($map == '') {
            $list = Db::name('order')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
            $count = Db::name('order')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
         } else {
            $list = Db::name('order')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
            $count = Db::name('order')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
         }

         foreach ($list as $key => $value) {
            $list[$key]['dj_time'] = date('Y-m-d', $value['dj_time']);
            $customer = Db::name('customer')->where('id', $value['user_id'])->find();
            $list[$key]['customer'] = $customer['sn'] . ' ' . $customer['name'];
            $list[$key]['saler'] = Db::name('auth_user')->where('id', $value['saler'])->value('name');
            $list[$key]['add_user'] = Db::name('auth_user')->where('id', $value['saler'])->value('name');
            switch ($value['o_type']) {
               case 0:
                  $list[$key]['o_type'] = '销售订单';
                  break;
               case 1:
                  $list[$key]['o_type'] = '销售订单';
                  break;
               case 2:
                  $list[$key]['o_type'] = '销售订单';
                  break;
               default:
                  # code...
                  break;
            }
            $list[$key]['number'] = Db::name('order_good')->where('order_id', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->sum('num');
            $list[$key]['jine'] = Db::name('order_good')->where('order_id', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->sum('jine');
            $list[$key]['zk_e'] = Db::name('order_good')->where('order_id', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->sum('zk_e');
            $list[$key]['tax_e'] = Db::name('order_good')->where('order_id', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->sum('tax_e');
            $list[$key]['total_price'] = Db::name('order_good')->where('order_id', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->sum('total_price');

            $order_details = Db::name('order_good')->where('order_id', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
            $list[$key]['xscb'] = 0;
            foreach ($order_details as $k => $v) {
               $list[$key]['xscb'] += Db::name('goods_price_strategy')->where('goods_id', $v['goods_id'])->value('estimated_purchase_price');
            }
            // 销售毛利
            $list[$key]['xsml'] = $list[$key]['jine'] - $list[$key]['xscb'];
            // 毛利率%
            $list[$key]['mlv'] = $list[$key]['xsml'] / $list[$key]['jine'];
            // 销售净利润
            $list[$key]['xsjlr'] = $list[$key]['xsml'] - $list[$key]['yh_e'] - $list[$key]['saler_user_price'];
            // 净利率
            $list[$key]['jll'] = $list[$key]['xsjlr'] / $list[$key]['jine'];

            // 应收-实收
            $list[$key]['ys'] = Db::name('user_money')->where('pid', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->where('type', 0)->sum('system_price');
            $list[$key]['ss'] = Db::name('user_money')->where('pid', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->where('type', 0)->sum('user_price');
         }

         // 添加商品明细
         $arr = [];
         if ($data['isGoodsDetails'] == 1) {
            foreach ($list as $key => $value) {
               array_push($arr, $list[$key]);
               $details = Db::name('order_good')->where('order_id', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
               foreach ($details as $k => $v) {
                  $goods = Db::name('goods')->where('id', $v['goods_id'])->find();
                  $details[$k]['goods_category'] = Db::name('goods_category')->where('id', $goods['goods_category_id'])->value('name');
                  $details[$k]['goods_sn'] = $goods['sn'];
                  $details[$k]['goods_name'] = $goods['name'];
                  $details[$k]['goods_model'] = $goods['model'];
                  $details[$k]['number'] = $v['num'];
                  $details[$k]['goods_dj'] = $v['price'];
                  $details[$k]['goods_hsdj'] = $v['hs_price'];
                  $details[$k]['goods_dwcb'] = Db::name('goods_price_strategy')->where('goods_id', $v['goods_id'])->value('estimated_purchase_price') * $v['num'];
                  array_push($arr, $details[$k]);
               }
            }
            $list = $arr;
         }
         // halt($arr);

         return $this->grid_data($list, $count, $page, $limit);
      }

      return $this->fetch();
   }


   public function add()
   {
      $data = $this->request->param();


      return $this->fetch();
   }
}
